# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Mateusz Mikula <mati865@gmail.com>

_realname=binutils
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.35
pkgrel=6
pkgdesc="A set of programs to assemble and manipulate binary and object files (mingw-w64)"
arch=('any')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
#checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' '!distcc' '!ccache') # 'debug' '!strip')
#install=binutils.install
source=(https://ftp.gnu.org/gnu/binutils/${_realname}-${pkgver}.tar.xz{,.sig}
        0001-enable-gold-on.mingw32.patch
        0002-check-for-unusual-file-harder.patch
        0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
        0020-binutils_2.31_mkdtemp_impl.patch
        0110-binutils-mingw-gnu-print.patch
        0120-windres-handle-spaces.patch
        1001-Change-the-default-characteristics-of-DLLs-built-by-.patch
        1002-mingw-plugin-test-regressions-due-to-commit-514b4e19.patch
        1003-Fixes-for-testsuite-failures-introduced-by-the-chang.patch
        1004-Pass-disable-reloc-section-on-PE-targets-for-PR-2566.patch
        2000-Temporarily-revert-default-dll-characteristics.patch
        2001-ld-option-to-move-default-bases-under-4GB.patch
        2002-Don-t-crash-if-asked-to-create-an-empty-implib.patch)
sha256sums=('1b11659fb49e20e18db460d44485f09442c8c56d5df165de9461eb09c8302f85'
            'SKIP'
            '93296b909e1a4f9d8a4bbe2437aafa17ca565ef6642a9812b0360c05be228c9d'
            '2c99345fc575c3a060d6677537f636c6c4154fac0fde508070f3b6296c1060d4'
            '604e76e0f702ced493ee22aa3c1768b4776b2008a7d70ae0dd35fe5be3522141'
            '34ba6c001ff7f95ae1da0619c73130112b76d0d2a31bb8a00602eb22f1f84cb8'
            '76658ef1bb8c5fc3fe6c26e2b5dd9ee0f1d12661988c0c65562b0a3e2d32ae1f'
            '86ae90d997e986a54aaebb5251f3a71800b0c5c3f5b57b9094a42995e9f5c478'
            '922c53a7f09790c74ec1aea6f96ade8bb32ecab22c2beccb24a45e15b960273b'
            '111a7385b93e9f1bf97503fe354ebe50f9be4b6d25cbb8678dde45df5e29ee8a'
            '57a61ac664027bed5da848a16af2ea3ad63e820d7db2d24588c33168d93276bb'
            'dc3724a9f42db77b38cb5db78aeaf3f92dbe8ea630db80e73135e9358d1e55c9'
            'a9b0e140a34032a67a051d693460b10c03f1a1361c210e687a214d0505deb494'
            'b790b09db892a1acde82bfe570ca52b50ed2ca98e6a99254cc8a8528eb87f8b2'
            'f37671f4d571d4b5477979798887d245c2e163a6e247591a01761b136c673eaa')
validpgpkeys=('EAF1C276A747E9ED86210CBAC3126D3B4AE55E93'
              '3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-enable-gold-on.mingw32.patch
  patch -p1 -i "${srcdir}"/0002-check-for-unusual-file-harder.patch
  patch -p1 -i "${srcdir}"/0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
  patch -p1 -i "${srcdir}"/0020-binutils_2.31_mkdtemp_impl.patch
  patch -p1 -i "${srcdir}"/0110-binutils-mingw-gnu-print.patch
  patch -p1 -i "${srcdir}"/0120-windres-handle-spaces.patch
  # Upstream patches
  patch -p1 -i "${srcdir}"/1001-Change-the-default-characteristics-of-DLLs-built-by-.patch
  patch -p1 -i "${srcdir}"/1002-mingw-plugin-test-regressions-due-to-commit-514b4e19.patch
  patch -p1 -i "${srcdir}"/1003-Fixes-for-testsuite-failures-introduced-by-the-chang.patch
  patch -p1 -i "${srcdir}"/1004-Pass-disable-reloc-section-on-PE-targets-for-PR-2566.patch

  # Temporarily revert defaults change while testing.
  patch -p1 -i "${srcdir}"/2000-Temporarily-revert-default-dll-characteristics.patch
  # Add an option to change default bases back below 4GB to ease transition
  patch -p1 -i "${srcdir}"/2001-ld-option-to-move-default-bases-under-4GB.patch
  # https://sourceware.org/bugzilla/show_bug.cgi?id=26588
  patch -p1 -i "${srcdir}"/2002-Don-t-crash-if-asked-to-create-an-empty-implib.patch
}

build() {
  if [ "${CARCH}" = "x86_64" ]; then
    local _conf='--enable-64-bit-bfd'
  else
    local _conf=''
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-werror \
    --enable-lto \
    --with-sysroot=${MINGW_PREFIX} \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    ${_conf} \
    --enable-nls \
    --disable-rpath \
    --disable-multilib \
    --enable-install-libiberty \
    --enable-plugins \
    --enable-gold \
    --disable-shared

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}

  # unset LDFLAGS as testsuite makes assumptions about which ones are active
  # do not abort on errors - manually check log files
  make LDFLAGS="" -k check || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{lib,include}/${_realname}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include
  cp ${pkgdir}${MINGW_PREFIX}/include/ansidecl.h ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/
  mv ${pkgdir}${MINGW_PREFIX}/include/*.h ${pkgdir}${MINGW_PREFIX}/include/${_realname}
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.a ${pkgdir}${MINGW_PREFIX}/lib/${_realname}

  # Add some useful headers
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/libiberty.h ${pkgdir}${MINGW_PREFIX}/include
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/demangle.h ${pkgdir}${MINGW_PREFIX}/include

  find ${pkgdir}${MINGW_PREFIX}/share -type f -iname "opcodes.mo" -o -iname "bfd.mo" | xargs -rtl1 rm

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING      ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING.LIB  ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING3     ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING3
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING3.LIB ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING3.LIB
}
